package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.apiResult.Result;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.vo.DownloadParkTicketVo;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.vo.ZsyyParkTicketVo;
import com.ruoyi.system.bo.ZsyyParkTicketQueryBo;
import com.ruoyi.system.bo.ZsyyParkTicketAddBo;
import com.ruoyi.system.bo.ZsyyParkTicketEditBo;
import com.ruoyi.system.service.IZsyyParkTicketService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 月票管理Controller
 *
 * @author wangghua
 * @date 2021-07-14
 */
@ApiIgnore
@Api(value = "月票管理控制器", tags = {"月票管理管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/parkTicket")
public class ZsyyParkTicketController extends BaseController {

    private final IZsyyParkTicketService iZsyyParkTicketService;
	@Autowired
	private TokenService tokenService;

    /**
     * 查询月票管理列表
     */
    @ApiOperation("查询月票管理列表")
    @PreAuthorize("@ss.hasPermi('system:parkTicket:list')")
    @GetMapping("/list")
    public TableDataInfo<ZsyyParkTicketVo> list(@Validated ZsyyParkTicketQueryBo bo) {
        return iZsyyParkTicketService.queryPageList(bo);
    }

    /**
     * 导出月票管理列表
     */
    @ApiOperation("导出月票管理列表")
    @PreAuthorize("@ss.hasPermi('system:parkTicket:export')")
    @Log(title = "月票管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<ZsyyParkTicketVo> export(@Validated ZsyyParkTicketQueryBo bo) {
        List<ZsyyParkTicketVo> list = iZsyyParkTicketService.queryList(bo);
        ExcelUtil<ZsyyParkTicketVo> util = new ExcelUtil<ZsyyParkTicketVo>(ZsyyParkTicketVo.class);
        return util.exportExcel(list, "月票管理");
    }

	/**
	 * 下载学生导入模板
	 */
	@ApiOperation("下载批量添加月票模板")
	@GetMapping("/importTemplate")
	public AjaxResult importTemplate()
	{
		ExcelUtil<DownloadParkTicketVo> util = new ExcelUtil<DownloadParkTicketVo>(DownloadParkTicketVo.class);
		return util.importTemplateExcel("批量添加月票模板");
	}


	/**
	 * 批量添加月票
	 */
	@ApiOperation("批量添加月票")
	@Log(title = "月票管理", businessType = BusinessType.IMPORT)
	@PreAuthorize("@ss.hasPermi('system:parkTicket:importData')")
	@PostMapping("/importData")
	public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
	{
		ExcelUtil<DownloadParkTicketVo> util = new ExcelUtil<DownloadParkTicketVo>(DownloadParkTicketVo.class);
		List<DownloadParkTicketVo> ticketVoList = util.importExcel(file.getInputStream());
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		String operName = loginUser.getUsername();
		String message = iZsyyParkTicketService.importParkTicket(ticketVoList,updateSupport, operName);
		return AjaxResult.success(message);
	}

    /**
     * 获取月票管理详细信息
     */
    @ApiOperation("获取月票管理详细信息")
    @PreAuthorize("@ss.hasPermi('system:parkTicket:query')")
    @GetMapping("/{id}")
    public AjaxResult<ZsyyParkTicketVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iZsyyParkTicketService.queryById(id));
    }

    /**
     * 新增月票管理
     */
    @ApiOperation("新增月票管理")
    @PreAuthorize("@ss.hasPermi('system:parkTicket:add')")
    @Log(title = "月票管理", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult add(@Validated @RequestBody ZsyyParkTicketAddBo bo) {
        return iZsyyParkTicketService.insertByAddBo(bo) ;
    }

    /**
     * 修改月票管理
     */
    @ApiOperation("修改月票管理")
    @PreAuthorize("@ss.hasPermi('system:parkTicket:edit')")
    @Log(title = "月票管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult edit(@Validated @RequestBody ZsyyParkTicketEditBo bo) {
        return iZsyyParkTicketService.updateByEditBo(bo);
    }

    /**
     * 删除月票管理
     */
    @ApiOperation("删除月票管理")
    @PreAuthorize("@ss.hasPermi('system:parkTicket:remove')")
    @Log(title = "月票管理" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return iZsyyParkTicketService.deleteWithValidByIds(Arrays.asList(ids));
    }
}
